package com.senditcertified.mail;

import org.json.JSONObject;

import com.Utility.Utility;
import com.activities.SecureActivity;
import com.senditcertified.mail.R;
import com.parser.Error_Report;
import com.parser.Parser_Soap;
import com.soap.call.Http_Soap_Call;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Voice extends Activity {
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	ProgressDialog pd;
	WebView webview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// TODO Put your code here
		setContentView(R.layout.video_att);
		webview = (WebView) findViewById(R.id.webview);
		ImageView downloader = (ImageView) findViewById(R.id.downloader);
		downloader.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				new User_file().execute("1");

			}
		});

		Button view = (Button) findViewById(R.id.view);
		view.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				new GetWebInterfaceURL().execute("1");

			}
		});

		Button copy_TO_Valut = (Button) findViewById(R.id.copy_TO_Valut);
		copy_TO_Valut.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				new add_Vault().execute("1");
			}
		});

		TextView file_1 = (TextView) findViewById(R.id.file_1);
		if (!TextUtils.isEmpty(SecureActivity.getEpackge.getVoice_msg())) {
			file_1.setVisibility(View.GONE);
			file_1.setText(SecureActivity.getEpackge.getVoice_msg());
			downloader.setVisibility(View.GONE);
			view.setVisibility(View.GONE);
			copy_TO_Valut.setVisibility(View.GONE);

			new GetWebInterfaceURL().execute("1");
		}

		pd = new ProgressDialog(
				((SecureActivity) getParent()).getParentActivity());
		pd.setMessage("Loading...");
		// TextView file_2 = (TextView) findViewById(R.id.file_2);
		// file_2.setText(TabSecure_Activity.getEpackge.getFile_name9());
	}

	class User_file extends AsyncTask<String, Void, Boolean> {
		boolean result = false;
		// String error_msg = "";
		String Result = null;
		Parser_Soap parse = new Parser_Soap();
		Error_Report repor = new Error_Report();

		@Override
		protected Boolean doInBackground(String... params) {
			Http_Soap_Call call = Http_Soap_Call.create_connection("",
					Voice.this);
			if (call != null) {
				Result = parse.GetFile(call.GetFile(Utility
						.getStringPreference(Voice.this, "LoginToken",
								params[0]), SecureActivity.MSG_ID, "1",
						repor), repor);
				if (Result != null) {
					System.out.println("true");
					if (Result.contains("Password Error")) {
						result = false;
					} else {
						result = true;
					}

				} else {
					// error_msg=Utility.INVALID_USER;
					System.out.println("false");
					result = false;
					repor.setMessage("Internal Error");
				}

			} else {
				repor.setMessage(Utility.NETWORK_ISSUE);
				return false;
			}

			return result;
		}

		@Override
		protected void onPreExecute() {
			/*
			 * pd = ProgressDialog.show(Attachment.this, null,
			 * getString(R.string.loader));
			 */
			handler.sendEmptyMessage(1);
		}

		protected void onPostExecute(Boolean result) {
			// This method runs in the UI thread
			handler.sendEmptyMessage(2);
			// pd.dismiss();
			if (result) {
				// Utility.printLog("Login Responce", "Login successfully");
				// Utility.setStringPreference(getParent(), "LoginToken",
				// Result);
				// new User_GetUserInbox().execute(Password);
				/*
				 * Intent mainIntent = new Intent(Password.this,
				 * TabHome_Activity.class);
				 * Password.this.startActivity(mainIntent);
				 * Password.this.finish();
				 */
				// Toast.makeText(Password.this, "Login token get",
				// Toast.LENGTH_SHORT)
				// .show();
			} else {
				// Utility.printLog("Login Responce", "Login Unsuccessfully");
				// Toast.makeText(Password.this, repor.getMessage(),
				// Toast.LENGTH_SHORT).show();
			}

		}
	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if (msg.what == 1) {
				pd.show();
			} else {
				pd.dismiss();
			}
		}
	};

	class add_Vault extends AsyncTask<String, Void, Boolean> {
		boolean result = false;
		// String error_msg = "";
		String Result = null;
		Parser_Soap parse = new Parser_Soap();
		Error_Report repor = new Error_Report();

		@Override
		protected Boolean doInBackground(String... params) {
			Http_Soap_Call call = Http_Soap_Call.create_connection("",
					Voice.this);
			if (call != null) {
				Result = parse.CopyFileToVault(call.CopyFileToVault(Utility
						.getStringPreference(Voice.this, "LoginToken",
								params[0]), SecureActivity.MSG_ID, "1",
						repor), repor);
				if (Result != null) {
					System.out.println("true");
					if (Result.contains("Password Error")) {
						result = false;
					} else {
						result = true;
					}

				} else {
					// error_msg=Utility.INVALID_USER;
					System.out.println("false");
					result = false;
					repor.setMessage("Internal Error");
				}

			} else {
				repor.setMessage(Utility.NETWORK_ISSUE);
				return false;
			}

			return result;
		}

		@Override
		protected void onPreExecute() {
			/*
			 * pd = ProgressDialog.show(Attachment.this, null,
			 * getString(R.string.loader));
			 */
			handler.sendEmptyMessage(1);
		}

		protected void onPostExecute(Boolean result) {
			// This method runs in the UI thread
			handler.sendEmptyMessage(2);
			// pd.dismiss();
			if (result) {

				try {
					JSONObject jsonObj = new JSONObject(Result);

					if (jsonObj.getString("ReturnValue") != null) {
						Toast.makeText(Voice.this,
								jsonObj.getString("ReturnValue"),
								Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(Voice.this,
								jsonObj.getString("ReturnValue"),
								Toast.LENGTH_SHORT).show();
					}

				} catch (Exception e) {
					e.printStackTrace();
				}

				// Utility.printLog("Login Responce", "Login successfully");
				// Utility.setStringPreference(getParent(), "LoginToken",
				// Result);
				// new User_GetUserInbox().execute(Password);
				/*
				 * Intent mainIntent = new Intent(Password.this,
				 * TabHome_Activity.class);
				 * Password.this.startActivity(mainIntent);
				 * Password.this.finish();
				 */
				// Toast.makeText(Password.this, "Login token get",
				// Toast.LENGTH_SHORT)
				// .show();
			} else {
				// Utility.printLog("Login Responce", "Login Unsuccessfully");
				// Toast.makeText(Password.this, repor.getMessage(),
				// Toast.LENGTH_SHORT).show();
			}

		}
	}

	class GetWebInterfaceURL extends AsyncTask<String, Void, Boolean> {
		boolean result = false;
		// String error_msg = "";
		String Result = null;
		Parser_Soap parse = new Parser_Soap();
		Error_Report repor = new Error_Report();

		@Override
		protected Boolean doInBackground(String... params) {
			Http_Soap_Call call = Http_Soap_Call.create_connection("",
					Voice.this);
			if (call != null) {
				Result = parse.GetWebInterfaceURL(call.GetWebInterfaceURL(
						Utility.getStringPreference(Voice.this, "LoginToken",
								params[0]), "Voice", SecureActivity.MSG_ID,
						"1", repor), repor);
				if (Result != null) {
					System.out.println("true");
					if (Result.contains("Password Error")) {
						result = false;
					} else {
						result = true;
					}

				} else {
					// error_msg=Utility.INVALID_USER;
					System.out.println("false");
					result = false;
					repor.setMessage("Internal Error");
				}

			} else {
				repor.setMessage(Utility.NETWORK_ISSUE);
				return false;
			}

			return result;
		}

		@Override
		protected void onPreExecute() {
			/*
			 * pd = ProgressDialog.show(Attachment.this, null,
			 * getString(R.string.loader));
			 */
			handler.sendEmptyMessage(1);
		}

		protected void onPostExecute(Boolean result) {
			// This method runs in the UI thread
			handler.sendEmptyMessage(2);
			// pd.dismiss();
			if (result) {
				parseWebInfo(Result);
			} else {
				// Utility.printLog("Login Responce", "Login Unsuccessfully");
				// Toast.makeText(Password.this, repor.getMessage(),
				// Toast.LENGTH_SHORT).show();
			}

		}
	}

	public void parseWebInfo(String result) {
		try {
			JSONObject onj = new JSONObject(result);
			String retValue = onj.getString("ReturnValue");
			System.out.println("WEBINFO " +retValue );
			if (retValue.startsWith("http") || retValue.startsWith("https"))  {
				/*
				 * Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(onj
				 * .getString("ReturnValue"))); startActivity(i);
				 */

				/*webview.setWebViewClient(new myWebclient());
				webview.getSettings().setJavaScriptEnabled(true);
				webview.getSettings().setPluginState(PluginState.ON);
				webview.getSettings().setPluginsEnabled(true);
				webview.getSettings().setAllowFileAccess(true);
				webview.requestDisallowInterceptTouchEvent(true);
				webview.loadUrl(retValue);*/
				startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(retValue)));
				/*((TabSecure_Activity) getParent()).scrollview.requestFocus();

				((TabSecure_Activity) getParent()).scrollview.scrollTo(0, 5);
				((TabSecure_Activity) getParent()).scrollview.postDelayed(
						new Runnable() {
							public void run() {
								((TabSecure_Activity) getParent()).scrollview
										.fullScroll(ScrollView.FOCUS_UP);
							}
						}, 300);
				
				((TabSecure_Activity) getParent()).scrollview.smoothScrollTo(0, 30);  */
			}

		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		System.out.println("Stop");
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		System.out.println("Ondestroy");
		super.onDestroy();

	}

	public class myWebclient extends WebViewClient

	{

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			// TODO Auto-generated method stub
			super.onPageStarted(view, url, favicon);
		}

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			// TODO Auto-generated method stub
			return super.shouldOverrideUrlLoading(view, url);
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
			webview.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}